package com.zhz.lib.project.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhz.lib.project.controller.form.LibnewsForm;
import com.zhz.lib.project.domain.news.Sortmap;
import com.zhz.lib.project.ibatis.domain.libnews.Libnews;
import com.zhz.lib.project.service.TypeService;
import com.zhz.lib.project.service.libnews.LibnewsService;
import com.zhz.lib.project.service.libnews.LibnewsUtil;
import com.zhz.lib.project.vo.libnews.LibnewsVO;
import com.zhz.project.common.util.hibernate.BaseDAO;
import com.zhz.project.common.util.ibatispage.IbatisPage;
import com.zhz.project.common.util.zhzutil.ZhzBeanUtils;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;

/**
 * @author LiWei <a href="mailto:liwei2672@gmail.com">liwei2672@gmail.com</a>
 * @version 创建时间：2010-5-3 下午03:22:27
 * 
 */
@Controller
@RequestMapping("/libnews.htm")
public class LibNewsController {
    @Autowired
    private LibnewsService libnewsService;

    @Autowired
    private BaseDAO        baseDao;
    @Autowired
    private TypeService    typeService;
    @Autowired
    private IbatisPage     ibatisPage;

    //分页时显示的标题长度
    private static int     MAX_LENGTH = 50;
    private static String  pageSql    = "selectAllLibnewsByPage";
    private static String  countSql   = "selectAllLibnewsCount";

    /**
     * 最新消息列表(Ibatis)
     * 
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, params = "action=page")
    public String getIbatisListPost(ModelMap model, LibnewsForm libnewsForm) {
        return page(model, libnewsForm);
    }

    /**
     * 最新消息列表(Ibatis)
     * 
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, params = "action=page")
    public String getIbatisListGet(ModelMap model, LibnewsForm libnewsForm) {
        return page(model, libnewsForm);
    }

    @SuppressWarnings("unchecked")
    private String page(ModelMap model, LibnewsForm libnewsForm) {
        ZhzHttpRequestUtil.writeMapToModelMap(model, ibatisPage.getPage(ZhzBeanUtils
            .describe(libnewsForm), countSql, pageSql));

        List<Libnews> libnewsList = (List<Libnews>) model.get("list");
        model.put("list", LibnewsUtil.ConvertTitle(libnewsList, MAX_LENGTH));
        Sortmap sortmap = (Sortmap) baseDao.findOneObject("Sortmap", "sortMapId", libnewsForm
            .getSortMapId());
        model.put("sortmap", sortmap);
        typeService.setToModelMap(model);
        return "list";
    }

    /**
     * 
     * 根据id获取最新消息
     * 
     * @param model
     * @param request
     * @param response
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(method = RequestMethod.GET, params = "action=single")
    public String getSingleNews(ModelMap model, @RequestParam("id") int id) {

        LibnewsVO libnewsVO = libnewsService.getLibnewsById(id);

        Sortmap sortmap = (Sortmap) baseDao.findOneObject("Sortmap", "sortMapId", libnewsVO
            .getSortType());

        libnewsVO.setLibnewsfrom(sortmap.getValue());

        model.put("sortmap", sortmap);
        model.put("libnews", libnewsVO);

        typeService.setToModelMap(model);
        return "libnewsDetail";
    }
}
